//
//  RegisterViewController.m
//  Lightthings
//
//  Created by Hero on 2024/11/15.
//

#import "RegisterViewController.h"
#import "RegisterSuccessAlert.h"
@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *codeView;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UIView *password2View;
@property (weak, nonatomic) IBOutlet UILabel *passwordTipLab;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF2;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UILabel *countryLab;

@property (weak, nonatomic) IBOutlet UIButton *pswShow;
@property (weak, nonatomic) IBOutlet UIButton *pswShow2;

/* <#note#> */
@property (nonatomic, strong) UILabel * agreeLab;
@property (nonatomic, strong) UIButton * agreeBtn;
@property (nonatomic, strong) CQCountDownButton * codeBtn;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLab.text = SWString(@"账号注册");
    self.passwordTipLab.text = SWString(@"*8-16位字符，包含字母、数字、符号中至少2种");
    
//    self.countryLab.text = SWString(@"国家/地区");
    
    self.phoneView.layer.borderColor = KLColor(@"#cccccc").CGColor;
    self.codeView.layer.borderColor = KLColor(@"#cccccc").CGColor;
    self.passwordView.layer.borderColor = KLColor(@"#cccccc").CGColor;
    self.password2View.layer.borderColor = KLColor(@"#cccccc").CGColor;
    
    self.phoneTF.placeholder = SWString(@"请输入手机号/邮箱号");
    self.codeTF.placeholder = SWString(@"请输入验证码");
    self.passwordTF.placeholder = SWString(@"请设置至少8位登录密码");
    self.passwordTF2.placeholder = SWString(@"请再次输入登录密码");
    self.phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.codeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordTF2.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordTF.secureTextEntry =  self.passwordTF2.secureTextEntry = YES;
    [self.registerBtn setTitle:SWString(@"注册") forState:0];
    [self agreeLab];
    
    KLWKSelf
    [self.codeBtn configDuration:4 buttonClicked:^{
        //========== 按钮点击 ==========//
        KLSTSelf
        [self.codeBtn startCountDown];
        if (self.phoneTF.text) {
        
    
        }
        else {
            [self.codeBtn endCountDown];
        }
    } countDownStart:^{
        // 倒计时开始
        NSLog(@"倒计时开始");
    } countDownUnderway:^(NSInteger restCountDownNum) {
        // 倒计时进行中
        KLSTSelf
        NSString *title = [NSString stringWithFormat:@"%lds", restCountDownNum];
        [self.codeBtn setTitle:title forState:UIControlStateNormal];
    } countDownCompletion:^{
        // 倒计时结束
        KLSTSelf
        [self.codeBtn setTitle:SWString(@"重新发送") forState:UIControlStateNormal];
        NSLog(@"倒计时结束");
    }];
    
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)clickRegister:(UIButton *)sender {
    
    RegisterSuccessAlert * alert= [[RegisterSuccessAlert alloc] init];
    [alert show];
    
}

- (IBAction)clickShowPassword:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.passwordTF.secureTextEntry = !sender.selected;
}
- (IBAction)clickShowPassword2:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.passwordTF2.secureTextEntry = !sender.selected;
}

- (UILabel *)agreeLab {
    if (!_agreeLab) {
        UILabel *obj = [UILabel new];
        [self.view addSubview:_agreeLab = obj];
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            kMakeL(self.agreeBtn.mas_right).offset(5);
            kMakeT(self.agreeBtn.mas_top);
            kMakeRV(20);
        }];
        obj.numberOfLines = 2;
        UIColor *color = JYColor_Main();
        UIFont *font = U_font(14);
        obj.font = font;
        obj.textColor = KLColor(@"#323233");
        obj.numberOfLines = 0;
        [obj setTotal:@"我已阅读并同意《隐私政策》和《服务协议》" stringArray:@[@"《隐私政策》", @"《服务协议》"] colorArray:@[color,color] fontArray:@[font,font] lineSpacing:1];
        obj.tapHighlightedColor = [UIColor clearColor];
        KLWKSelf
        [obj yb_addAttributeTapActionWithStrings:@[@"《隐私政策》", @"《服务协议》"] tapClicked:^(UILabel * _Nonnull label, NSString * _Nonnull string, NSRange range, NSInteger index) {
            KLSTSelf
            if (index==0) {
                
            }
            else if (index==1){
                
            }
        }];
    }
    return _agreeLab;
}

- (UIButton *)agreeBtn {
    if (!_agreeBtn) {
        UIButton *obj = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:_agreeBtn = obj];
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            kMakeWHV(20, 20);
            kMakeL(self.password2View.mas_left);
            kMakeT(self.password2View.mas_bottom).offset(15);
        }];
        [obj setImage:U_imageNamed(@"协议选框_未勾选") forState:UIControlStateNormal];
        [obj setImage:U_imageNamed(@"协议选框_勾选") forState:UIControlStateSelected];
        [obj addTarget:self action:@selector(clickSelected) forControlEvents:UIControlEventTouchUpInside];
    }
    return _agreeBtn;
}

- (void)clickSelected {
    self.agreeBtn.selected = !self.agreeBtn.selected;
}
- (CQCountDownButton *)codeBtn {
    if (!_codeBtn) {
        CQCountDownButton *obj = [CQCountDownButton buttonWithType:UIButtonTypeCustom];
        [self.codeView addSubview:_codeBtn = obj];
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            kMakeWV(100);
            kMakeTV(10);
            kMakeBV(-10);
            kMakeRV(-10);
        }];
        obj.titleLabel.font = U_font(14);
        obj.backgroundColor = JYColor_Main();
        obj.layer.cornerRadius = 15;
        obj.layer.masksToBounds = YES;
        [obj setTitle:SWString(@"获取验证码") forState:UIControlStateNormal];
    }
    return _codeBtn;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
